package com.lixin.juc.functioninterface;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class MainApplication {

    public static void main(String[] args) {
        //testFunction();
        //testPredicate();
        //testConsumer();
        //testSupplier();
        testStream();
    }

    // 流计算
    public static void testStream() {
        User user1 = new User(1L, "a", 21);
        User user2 = new User(2L, "b", 22);
        User user3 = new User(3L, "c", 23);
        User user4 = new User(4L, "d", 24);
        User user5 = new User(5L, "e", 25);
        User user6 = new User(6L, "f", 26);
        List<User> userList = Arrays.asList(user1, user2, user3, user4, user5, user6);
        userList.stream().filter(u -> { return u.getId() % 2 == 0 && u.getAge() > 23; })
                .map(u -> { return u.getName().toUpperCase(); })
                .sorted((u1, u2) -> { return u2.compareTo(u1); })
                .limit(1)
                .forEach(System.out::println);
    }

    // 供养型接口：无输入值，有返回值
    public static void testSupplier() {
        /*Supplier<String> supplier = new Supplier<String>() {
            @Override
            public String get() {
                return "爱的供养";
            }
        };*/
        Supplier<String> supplier = () -> { return "爱的供养"; };
        System.out.println(supplier.get());
    }

    // 消费型接口：有一个输入值，但无返回值
    public static void testConsumer() {
       /* Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };*/
        //Consumer<String> consumer = (s) -> { System.out.println(s); };
        //Consumer<String> consumer = s -> { System.out.println(s); };
        Consumer<String> consumer = System.out::println;
        consumer.accept("巨人搞笑");
    }

    // 函数型接口：有一个输出值，有一个返回值
    public static void testFunction() {
        /*Function<Character, Integer> func = new Function<Character, Integer>() {
            @Override
            public Integer apply(Character c) {
                return Integer.valueOf(c);
            }
        };*/
        //Function<Character, Integer> func = (c) -> { return Integer.valueOf(c); };
        //Function<Character, Integer> func = c -> { return Integer.valueOf(c); };
        Function<Character, Integer> func = Integer::valueOf;
        System.out.println(func.apply('A'));
    }

    // 断言型接口：有一个输入值，返回布尔值
    public static void testPredicate() {
        /*Predicate<String> pre = new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.isEmpty();
            }
        };*/
        //Predicate<String> pre = (s) -> { return s.isEmpty(); };
        //Predicate<String> pre = s -> { return s.isEmpty(); };
        Predicate<String> pre = String::isEmpty;
        System.out.println(pre.test(""));
    }

}
